package com.jzhao.gper.demo.aspect;

import com.jzhao.gper.spring.v3.aop.intercept.JZJoinPoint;
import com.jzhao.gper.spring.v3.aop.intercept.JZMethodInvocation;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class LogAspect {

    public void before(JZJoinPoint jp){
        JZMethodInvocation mi = (JZMethodInvocation) jp;
        mi.setUserAttribute("startTime",new Date());
        Object target = jp.getThis();
        log.info("joinpoint target="+target.getClass().getSimpleName()+"."+mi.getMethod().getName());
        log.info("Invoker Before Method!!!");
    }

    public void after(JZJoinPoint jp){
        JZMethodInvocation mi = (JZMethodInvocation) jp;
        Date startTime = (Date) mi.getUserAttribute("startTime");
        long useTime= new Date().getTime() - startTime.getTime();
        log.info("方法执行用时：" + useTime);
        log.info("Invoker After Method!!!");
    }

    public void afterThrowing(JZJoinPoint jp,Throwable ex){
        log.error("异常信息：" + ex.getMessage());
        log.info("Invoker AfterThrowing Method!!!");
    }
}
